<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">用户管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>用户管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="user-search-key">
                <option></option>
                <option value="id">ID</option>
                <option value="username">账号</option>
                <option value="nickName">用户名</option>
                <option value="phone">手机号</option>
            </select>&emsp;
            <input id="user-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="user-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            </button>
        </div>

        <table class="layui-table" id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<!-- 表头操作列 -->
<script type="text/html" id="user-table-bar">
    <div class="layui-inline" lay-event="add" lay-tips="新增，新用户密码为 123456"><i class="layui-icon layui-icon-add-1"></i></div>
    <div class="layui-inline" lay-event="edit" lay-tips="修改"><i class="layui-icon layui-icon-edit"></i></div>
    <div class="layui-inline" lay-event="del" lay-tips="删除"><i class="layui-icon layui-icon-delete"></i></div>
    <div class="layui-inline" lay-event="resetPwd" lay-tips="重置密码"><i class="layui-icon layui-icon-password"></i></div>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="user-tpl-state">
    <input type="checkbox" lay-filter="user-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定" {{d.state===0?'checked':''}}/>
</script>

<script>
    layui.use(['form', 'table', 'util', 'admin', 'jquery', 'index'], function () {
        const $ = layui.jquery;
        const form = layui.form;
        const table = layui.table;
        const layer = layui.layer;
        const admin = layui.admin;
        const index = layui.index;

        form.render('select');

        // 注意表格渲染的时候，两个中括号‘[ [ ] ]’一定要分开写，因为与thymeleaf的表达式冲突
        // 渲染表格
        table.render({
            title: '用户数据列表',
            elem: '#user-table',
            url: basePath + 'system/user/list',
            toolbar: '#user-table-bar',
            defaultToolbar: ['filter'],
            page: true,
            cols: [
                [
                    {type: 'radio', fixed: 'left'},
                    {field: 'id', sort: true, title: '编号', hide: true},
                    {field: 'username', sort: true, title: '账号'},
                    {field: 'nickName', sort: true, title: '用户名'},
                    {field: 'phone', sort: true, title: '手机号'},
                    {field: 'sex', sort: true, title: '性别'},
                    {field: 'createDate', sort: true, title: '创建时间'},
                    {field: 'state', sort: true, templet: '#user-tpl-state', title: '状态'}
                ]
            ]
        });

        //头工具栏事件
        table.on('toolbar(user-table)', function (obj) {
            const checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            const data = checkStatus.data;  //获取选中行数据
            console.info(JSON.stringify(data));

            switch (obj.event) {
                case 'add':
                    addOrEdit();
                    break;
                case 'edit':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                        return;
                    }
                    addOrEdit(data[0]);
                    break;
                case 'del':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                        return;
                    }
                    del(data[0].id);
                    break;
                case 'resetPwd':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                        return;
                    }
                    restPwd(data[0].id);
                    break;
            }

        });

        const addOrEdit = function (data) {
            let title = data ? '修改用户' : '添加用户';
            admin.putTempData('t_user', data);
            admin.putTempData('t_title', title);
            admin.popupCenter({
                title: title,
                path: basePath + 'system/user/editForm',
                finish: function () {
                    table.reload('user-table', {});
                }
            });
        };

        const del = function (id) {
            layer.confirm('确定要删除吗？', function () {
                layer.load(2);
                $.post(basePath + 'system/user/delete', {
                    userId: id
                }, function (data) {
                    layer.closeAll('loading');
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        table.reload('user-table', {});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                });
            });
        };

        /**
         * 重置密码
         * @param id
         */
        const restPwd = function (id) {
            layer.confirm('确定重置此用户的密码吗？', function (i) {
                layer.close(i);
                layer.load(2);
                $.post(basePath + 'system/user/restPwd', {
                    userId: id
                }, function (data) {
                    layer.closeAll('loading');
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                });
            });
        };

        // 搜索按钮点击事件
        $('#user-btn-search').click(function () {
            const key = $('#user-search-key').val();
            const value = $('#user-search-value').val();
            table.reload('user-table', {where: {searchKey: key, searchValue: value}});
        });

        // 修改user状态
        form.on('switch(user-tpl-state)', function (obj) {
            layer.load(2);
            $.post(basePath + 'system/user/updateState', {
                userId: obj.elem.value,
                state: obj.elem.checked ? 0 : 1
            }, function (data) {
                layer.closeAll('loading');
                if (data.success) {
                    layer.msg(data.msg, {icon: 1});
                    //table.reload('table-user', {});
                } else {
                    layer.msg(data.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            });
        });
    });
</script>